/*********************** compute statistics by group *********************/
cap program drop get_rd_teststat_for_fisher_test
program get_rd_teststat_for_fisher_test
	version 18.5
	set seed 1028
	args bidder_auction quantilex
	*clear
	set type double
	do modules/__rd_computation.do
	keep MDYbid ldelta1* tie* firm_treatment g28_plus rank1* FLAG fy_con id percent_reserve ftt_ts1* regionfirmID ftt_N* tsrank
	qui gen after = 1 if MDYbid >= td(15mar2019) & MDYbid < td(01apr2021)
	qui replace after = 0 if MDYbid >= td(01apr2015) & MDYbid < td(13feb2019)
	qui sort regionfirmID ftt_ts1s
	qui by regionfirmID: replace ftt_ts1s = ftt_ts1s[_n-1] if ftt_ts1s == .
	qui sort regionfirmID ftt_ts1p
	qui by regionfirmID: replace ftt_ts1p = ftt_ts1p[_n-1] if ftt_ts1p == .
	qui sort regionfirmID ftt_N1
	qui by regionfirmID: replace ftt_N1 = ftt_N1[_n-1] if ftt_N1 == .
	qui sort regionfirmID tsrank
	qui by regionfirmID: replace tsrank = tsrank[_n-1] if tsrank == .
	
	pause on

	foreach runvar in "score" "price"{
		qui gen condition_`runvar' = ""
		qui gen tau_`runvar' = .
		qui gen tstat_`runvar' = .
		qui gen N_`runvar' = .
		qui gen N_auctions_`runvar' = .
		qui gen h_r_`runvar' = .
		qui gen h_l_`runvar' = .
		qui gen b_r_`runvar' = .
		qui gen b_l_`runvar' = .
	}
	foreach period in "after" "before"{
		local st_fy = 2011
		local en_fy = .
		di "****** Before/After: `period' ******"
		if "`period'" == "before"{
			local st_mdybid = "td(1apr2015)"
			local ed_mdybid = "td(12feb2019)"
		}
		else if "`period'" == "after"{
			local st_mdybid = "td(15mar2019)"
			local ed_mdybid = "td(31mar2021)"
		}
		pause on
		forvalues x = 1(1)28{
			if `x' != 21 & `x' != 24{
				local y = strofreal(`x', "%02.0f")
				foreach runvar in "score" "price" {
					cap drop thebidder
					qui gen thebidder = 1 if g28_plus == `x' & firm_treatment < .
					
					if "`runvar'" == "score"{
						local leftright = "l"
						local outcomevar = "ldelta1p_s"
						local runningvar = "ldelta1s_s"
						local sp = "s"
					}
					else{
						local leftright = "r"		
						local outcomevar = "ldelta1s_p"
						local runningvar = "ldelta1p_p"
						local sp = "p"
					}
				
					
					cap drop theauction
					if "`bidder_auction'" == "auction"{
						qui bys id: egen theauction = max( thebidder == 1 )
					}
					else{
						gen theauction = thebidder
					}
					/*if "`quantilex'" ~= ""{
						qui sum percent_reserve if theauction == 1, d
						qui replace theauction = 0 if percent_reserve < `r(`quantilex')'
						local _quantile = "_`quantilex'"
					}
					*/
					qui count if theauction == 1	
					if r(N) > 0{
						di "$$$$$$ group: `y'; Period: `period'; runvar: `runvar' $$$$$$$"
						local condition_plus = "& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid' "
						if "`runvar'" == "score"{
							*local test_price_score = "& ftt_ts1s < . "	/*added on 6Nov23*/
							* for nprobust local condition_plus = "`condition_plus' & ldelta1s_s < 0"
						}
						else{
							*local test_price_score = "& ftt_ts1p < ."				
							* for nprobust local condition_plus = "`condition_plus' & ldelta1p_p > 0"
						}
						local obs = .
						local success = 0
						local N_auctions = .

						preserve
							/* Using rdrobust with rdbwselect */
							qui replace ldelta1p_s = -ldelta1s_s if rank1_s == 1
							qui replace ldelta1s_p = -ldelta1p_p if rank1_p == 1
							/*scatter `outcomevar' `runningvar' if tie1`sp' ~= 1 ///
								& (theauction == 1 | rank1_`sp' == 1 ) & `condition_plus'*/
							*pause
							local trim = 0
							local condition0 = " `condition_plus'`test_price_score'"
							
							qui count if tie1`sp' ~= 1 &(theauction == 1 & rank1_`sp' == 1) `condition0' 
							local N_auctions = `r(N)'
							
							/*** reproducibility issue ***/
							/* For reproducibility, estimates for a some groups ///
							are dropped in the subsequent code. This is because of the following reason. ///
							In the original code we used fewer retries in the rdrobust estimaton, with a ///
							trimming size starting 0.002 and capped at 0.03. We were using /
							AMD Ryzen 7 7840U to run the code, and the following groups failed to estimate: ///
							test 1 (running variable = score): groups 13, 14, 25, and ///
							26;  test 2 (running variable = price): groups 5, 13, 18, ///
							and 25. In preparing our replication package, we encountered ///
								a hardware dependent descrepancy in the results; ///
							On an Intel Core i5-8250Um, it failed to compute ///
							groups 5, 16, 27, 28 for test 1  groups ///
							3, 6, 8, 9, 11, 19 for test 2 in addition to those that ///
							AMD failed to compute. To address this discrepancy, we increased the ///
							number of retries and used smaller trimming sizes (i.e., ///
							0.0002) at each retry. Despite this update, we still ///
							observed failures for group 23 in the post on Intel (but not AMD). In other words, we could not resolve the hard-ward dependent
							discrepancy for group 23.///
							We decided to drop group 23 for the final results. ***/
							
							if ("`runvar'" == "price") & ( ///
									("`period'" == "before" & `y' == 28)| ///
									("`period'" == "after" & ( ///
										`y' == 5|  ///
										`y' == 13| ///
										`y' == 18| ///
										`y' == 23| ///
										`y' == 25) ///
									) ///
								)| ///
								("`runvar'" == "score" & ( ///
									("`period'" == "before" & (`y' == 25|`y' == 26))| ///
									("`period'" == "after" & ( ///
										`y' == 13|  ///
										`y' == 14| ///
										`y' == 25| ///
										`y' == 26) ///
									))) ///
							{ 
								di as result "Estimation `runvar'_`period' Skip"
								local trim = 1
							}
													
								
							while `trim' <.005 {				/* In the accepted version of the paper, we used 0.03 */						
								cap noisily rdrobust_bwselect "`outcomevar'" "`runningvar'" ///
									"if tie1`sp' ~= 1 & ((theauction == 1&rank1_`sp'!=1)|(rank1_`sp'==1&abs(`runningvar')>`trim'))`condition0'" /// 
									"`leftright'" "msetwo"
								di as result "r(est)：`r(est)'"
								
								if "`r(est)'" == ""{
									sleep 20									
								}
								else if `r(est)'< . {
									local success = 1
								}

								if `success' == 1{
									local trim = 1
									local obs = `r(N)'
								}
								else{
									qui count if tie1`sp' ~= 1 & (theauction == 1 & rank1_`sp' > 1) ///
									`condition_plus'`test_price_score'
									local obs = `r(N)'
									local trim = `trim' + .0002	/* In the accepted version of the paper, we used 0.002 instead of 0.0002 */
									di "TTTTTTTTTT trimming_size: `trim'; N = `obs' TTTTTTTTTTT"
								}
							}

							if `success' == 1  {
								local est = string(`r(est)', "%6.3f" )
								local tstat = string(`r(tstat)', "%6.3f" )
								local N = string(`obs', "%6.0f" )
								local rest = `r(est)'
								local rtstat = `r(tstat)'

								local h_l = `r(h_l)'
								local b_l = `r(b_l)'

							}
						restore
						
						if `success' == 1{
							di "**** rd (`bidder_auction'; `runvar'); est:`est'; tstat:`tstat'; N:`N' ****"
							qui replace tau_`runvar' = `rest' if thebidder == 1 ///
								& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid'
							qui replace tstat_`runvar' = `rtstat' if thebidder == 1 ///
								& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid'
							qui replace N_`runvar' = `obs' if thebidder == 1 ///
								& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid'		
							qui replace N_auctions_`runvar' = `N_auctions' if thebidder == 1 ///
								& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid'
							qui replace h_l_`runvar' = `h_l' if thebidder == 1 ///
								& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid'
							qui replace b_l_`runvar' = `b_l' if thebidder == 1 ///
								& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid'
						}
						qui replace condition_`runvar' = "`period'_group:`y'" if thebidder == 1 ///
							& MDYbid >= `st_mdybid' & MDYbid <= `ed_mdybid'
					}
				}
			}
		}
	}

	**************************************************************
	*save temp0000.dta, replace

	*pause

	replace condition_score = condition_price if condition_score == ""
	replace condition_price = condition_score if condition_price == ""
	sort condition_score tau_score
	by condition_score: replace tau_score = tau_score[_n-1] if tau_score == . & _n > 1
	by condition_score: replace tstat_score = tstat_score[_n-1] if tstat_score == . & _n > 1
	by condition_score: replace N_score = N_score[_n-1] if N_score == . & _n > 1
	by condition_score: replace N_auctions_score = N_auctions_score[_n-1] if N_auctions_score == . & _n > 1
	by condition_score: replace h_l_score = h_l_score[_n-1] if h_l_score == . & _n > 1
	by condition_score: replace h_r_score = h_r_score[_n-1] if h_r_score == . & _n > 1
	by condition_score: replace b_l_score = b_l_score[_n-1] if b_l_score == . & _n > 1
	by condition_score: replace b_r_score = b_r_score[_n-1] if b_r_score == . & _n > 1
	
	sort condition_price tau_price
	by condition_price: replace tau_price = tau_price[_n-1] if tau_price == . & _n > 1
	by condition_price: replace tstat_price = tstat_price[_n-1] if tstat_price == . & _n > 1
	by condition_price: replace N_price = N_price[_n-1] if N_price == .	 & _n > 1
	by condition_price: replace N_auctions_price = N_auctions_price[_n-1] if N_auctions_price == .	 & _n > 1
	by condition_price: replace h_l_price = h_l_price[_n-1] if h_l_price == . & _n > 1
	by condition_price: replace h_r_price = h_r_price[_n-1] if h_r_price == . & _n > 1
	by condition_price: replace b_l_price = b_l_price[_n-1] if b_l_price == . & _n > 1
	by condition_price: replace b_r_price = b_r_price[_n-1] if b_r_price == . & _n > 1
	
	
	*save temp00.dta, replace

	keep if fy_con >= 2015 & fy_con <= . & FLAG == 0
	*egen conditionTG = tag(condition_score) if g28_plus != 21 | g28_plus != 24 
	egen condition_TG = tag(condition_score)
	
	*keep if condition_price != ""
	keep if condition_TG == 1
	keep g28_plus condition_score condition_price tau_* tstat_* N_* h_* b_*
	save ../fishertest/rd_`bidder_auction'`_quantile'.dta, replace
end		
		